package com.galgram.project.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import com.galgram.project.domain.FxCourseCurriculumChapter;
import com.galgram.project.service.IFxCourseChapterService;
import com.galgram.project.service.IFxCourseSectionService;
import com.galgram.project.service.IcourseCurriculumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxCourseChapterSection;
import com.galgram.project.service.IFxCourseChapterSectionService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 课程章节-小节关联Controller
 *
 * @author galgram
 * @date 2024-11-21
 */
@RestController
@RequestMapping("/project/courseChapterSection")
public class FxCourseChapterSectionController extends BaseController {
    @Autowired
    private IFxCourseChapterSectionService fxCourseChapterSectionService;
    @Autowired
    private IFxCourseChapterService fxCourseChapterService;
    @Autowired
    private IFxCourseSectionService fxCourseSectionService;

    /**
     * 查询课程章节-小节关联列表
     */
    @PreAuthorize("@ss.hasPermi('project:courseChapterSection:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxCourseChapterSection fxCourseChapterSection) {
        startPage();
        List<FxCourseChapterSection> list = fxCourseChapterSectionService.selectFxCourseChapterSectionList(fxCourseChapterSection);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setChapterName(fxCourseChapterService.getById(list.get(i).getChapterId()).getChapterName());
            list.get(i).setSectionName(fxCourseSectionService.getById(list.get(i).getSectionId()).getSectionName());
        }
        return getDataTable(list);
    }

    /**
     * 获取课程章节-小节关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:courseChapterSection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxCourseChapterSectionService.getById(id));
    }

    /**
     * 新增课程章节-小节关联
     */
    @PreAuthorize("@ss.hasPermi('project:courseChapterSection:add')")
    @Log(title = "课程章节-小节关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxCourseChapterSection fxCourseChapterSection) {
        fxCourseChapterSectionService.save(fxCourseChapterSection);
        return success("添加成功");
    }

    /**
     * 修改课程章节-小节关联
     */
    @PreAuthorize("@ss.hasPermi('project:courseChapterSection:edit')")
    @Log(title = "课程章节-小节关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxCourseChapterSection fxCourseChapterSection) {
        fxCourseChapterSectionService.updateById(fxCourseChapterSection);
        return success("修改成功");
    }

    /**
     * 删除课程章节-小节关联
     */
    @PreAuthorize("@ss.hasPermi('project:courseChapterSection:remove')")
    @Log(title = "课程章节-小节关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxCourseChapterSectionService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出课程章节-小节关联列表
     */
    @PreAuthorize("@ss.hasPermi('project:courseChapterSection:export')")
    @Log(title = "课程章节-小节关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxCourseChapterSection fxCourseChapterSection) {
        List<FxCourseChapterSection> list = fxCourseChapterSectionService.selectFxCourseChapterSectionList(fxCourseChapterSection);
        ExcelUtil<FxCourseChapterSection> util = new ExcelUtil<FxCourseChapterSection>(FxCourseChapterSection.class);
        util.exportExcel(response, list, "课程章节-小节关联数据");
    }

    /**
     * 导入课程章节-小节关联列表
     */
    @PreAuthorize("@ss.hasPermi('project:courseChapterSection:import')")
    @Log(title = "课程章节-小节关联", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxCourseChapterSection> util = new ExcelUtil<FxCourseChapterSection>(FxCourseChapterSection.class);
        List<FxCourseChapterSection> userList = util.importExcel(file.getInputStream());
        fxCourseChapterSectionService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:courseChapterSection:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxCourseChapterSection> list = new ArrayList<FxCourseChapterSection>();
        ExcelUtil<FxCourseChapterSection> util = new ExcelUtil<FxCourseChapterSection>(FxCourseChapterSection.class);
        util.exportExcel(response, list, "课程章节-小节关联数据");
    }
}
